//    Copyright (C) Kherty.  All rights reserved.
namespace OpenLS.Spreadsheet.Extensibility
{
    public class AllowToComplexConversionAttribute: ParameterConverterAttribute
    {
        internal override void ConvertImp(ConversionData data)
        {
            if (data.TargetType == typeof(Complex))
            {
                if (data.Value is Complex)
                {
                    data.Result = data.Value;
                    return;
                }
                if (data.Value is string)
                {
                    string s = (string)data.Value;
                    Complex c;
                    if (Complex.TryParse(s, out c))
                    {
                        data.Result = c;
                        return;
                    }
                }
                if (data.Value is double)
                {
                    data.Result = new Complex((double)data.Value, 0);
                    return;
                }
                data.Error = Errors.NumError;
            }
        }
    }
}
